<template>
  <el-dialog
    :visible="visible"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    title="选择文件"
    append-to-body
    destroy-on-close
    width="500px"
    custom-class="com-excel-upload"
    @close="handleClose"
  >
    <el-upload
      ref="upload"
      :action="action"
      :limit="1"
      :auto-upload="false"
      :file-list="fileList"
      :headers="extraheaders"
      :on-success="handleOnSuccess"
      :on-error="handleOnError"
      drag
    >
      <i class="el-icon-upload" />
      <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em>
        <div>只能上传Excel文件，单次仅限导入1个文件</div>
      </div>
      <!-- <div slot="tip" class="el-upload__tip"></div> -->
    </el-upload>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="handleClose">取 消</el-button>
      <el-button v-if="show" size="small" type="primary" @click="downloadTemplate">下载导入模板</el-button>
      <el-button size="small" type="primary" @click="handleSubmit">导 入</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getToken } from '@/utils/auth'

export default {
  props: {
    action: {
      type: String,
      default: ''
    },
    headers: {
      type: Object,
      default: () => {}
    },
    downloadTemplate: {
      type: Function,
      default: () => {}
    },
    downErrorExcel: {
      type: Function,
      default: () => {}
    },
    show: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      visible: false,
      extraheaders: {
        ...this.headers,
        token: getToken(),
        currentOrgId: this.$store.state.user.orgId
      },
      fileList: []
    }
  },
  watch: {
    headers(val) {
      this.extraheaders = {
        ...this.headers,
        token: getToken(),
        currentOrgId: this.$store.state.user.orgId
      }
    }
  },
  mounted() {
    this.visible = true
  },
  methods: {
    handleClose() {
      this.$emit('close')
    },
    handleSubmit() {
      // this.$emit('sure')
      this.$refs.upload.submit()
    },
    handleOnSuccess({ status, message, data }) {
      if (status) {
        this.$msgBox.show(message || '导入成功，请稍后查看')
      } else {
        data ? this.handleErrorExcel(message) : this.$msgBox.error(message)
      }
      this.$refs.upload.clearFiles()
      this.fileList = []
    },
    handleOnError({ message, data }) {
      data ? this.handleErrorExcel(message) : this.$msgBox.error(message)
      this.$refs.upload.clearFiles()
      this.fileList = []
    },
    handleErrorExcel(id) {
      this.$confirm('导入失败，是否下载导出错误excel?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        console.log('???', this.downErrorExcel)
        this.downErrorExcel(id)
      }).catch(() => {
      })
    }
  }
}
</script>

<style lang="scss">
.com-excel-upload {
  .el-dialog__body {
    text-align: center;
  }
  .el-dialog__title {
    color: #86bfff;
  }
}
</style>
